package com.example.librarysystem.Controller;

import com.example.librarysystem.Service.AdminService;
import com.example.librarysystem.Service.BorrowerService;
import com.example.librarysystem.entity.Admin;
import com.example.librarysystem.entity.Borrower;
import com.example.librarysystem.entity.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@Api(value = "注册登录接口")
public class LoginController {
    @Autowired
    private BorrowerService borrowerService;
    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "登录页面")
    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @ApiOperation(value = "读者登录请求")
    @PostMapping("/borrowerLogin")
    @ResponseBody
    public R borrowerLogin(HttpServletRequest request,
                           @RequestParam("name") String name,
                           @RequestParam("password") String password){
        Borrower borrower = borrowerService.selectByName(name);
        if(borrower == null){
            return R.fail("用户名不存在");
        }else {
            if(borrower.getPassword().equals(password)){
                request.getSession().setAttribute("isLogin", "1");
                request.getSession().setAttribute("id", borrower.getId());
                request.getSession().setAttribute("name", name);
                request.getSession().setAttribute("password", password);
                return R.ok();
            }else {
                return R.fail("密码错误");
            }
        }
    }
    @ApiOperation(value = "管理员登录请求")
    @PostMapping("/adminLogin")
    @ResponseBody
    public R adminLogin(HttpServletRequest request,
                           @RequestParam("name") String name,
                           @RequestParam("password") String password){
        Admin admin = adminService.selectByName(name);
        if(admin == null){
            return R.fail("管理员不存在");
        }else {
            if(admin.getPassword().equals(password)){
                request.getSession().setAttribute("isLogin", "2");
                request.getSession().setAttribute("id", admin.getId());
                request.getSession().setAttribute("name", name);
                request.getSession().setAttribute("password", password);
                return R.ok();
            }else {
                return R.fail("密码错误");
            }
        }
    }
    @ApiOperation(value = "注销请求")
    @GetMapping("/logout")
    @ResponseBody
    public R logout(HttpServletRequest request){
        request.getSession().removeAttribute("isLogin");
        request.getSession().removeAttribute("name");
        request.getSession().removeAttribute("password");
        request.getSession().removeAttribute("id");
        return R.ok();
    }
    @ApiOperation(value = "读者主页面")
    @GetMapping("/borrowerIndex")
    public String borrowerIndex(Model model, HttpServletRequest request){
        HttpSession session=request.getSession();
        String isLogin=(String)session.getAttribute("isLogin");
        String username=(String)session.getAttribute("name");
        model.addAttribute("isLogin",isLogin);
        model.addAttribute("name",username);
        return "borrower/borrowerIndex";
    }
    @ApiOperation(value = "管理员主页面")
    @GetMapping("/adminIndex")
    public String adminIndex(Model model, HttpServletRequest request){
        HttpSession session=request.getSession();
        String isLogin=(String)session.getAttribute("isLogin");
        String username=(String)session.getAttribute("name");
        model.addAttribute("isLogin",isLogin);
        model.addAttribute("name",username);
        return "admin/adminIndex";
    }
    @ApiOperation(value = "注册请求")
    @PostMapping("/register")
    @ResponseBody
    public R register(@RequestParam("name") String name,@RequestParam("password") String password,
                      @RequestParam("email") String email,@RequestParam("phoneNum") String phoneNum,
                      @RequestParam("address") String address){
        Borrower borrower = new Borrower();
        borrower.setPassword(password);
        borrower.setEmail(email);
        borrower.setPhoneNum(phoneNum);
        borrower.setAddress(address);
        borrower.setName(name);
        borrower.setRegisterDate(new Date());
        borrower.setBreach(false);
        if(borrowerService.insertBorrower(borrower) == 0){
            return R.ok();
        }
        return R.fail("请稍后再试");
    }
}
